package com.example.hzq;

import com.lmax.disruptor.WorkHandler;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Disruptor事件对象，用于传递网络消息
 */
public class MessageEvent implements WorkHandler<MessageEvent> {
    private static final Logger logger = LoggerFactory.getLogger(MessageEvent.class);
    private String message;
    private ChannelHandlerContext ctx;
    
    public void clear() {
        message = null;
        ctx = null;
    }
    
    // Getters and Setters
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public ChannelHandlerContext getCtx() {
        return ctx;
    }
    
    public void setCtx(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    @Override
    public void onEvent(MessageEvent event) throws Exception {
        // 处理接收到的消息
        ChannelHandlerContext ctx = event.getCtx();
        String message = event.getMessage();

        if (ctx != null && message != null) {
            logger.info("处理消息: {}", message);

            // 这里可以添加业务逻辑处理

            // 示例：简单回复客户端
            ctx.writeAndFlush("已收到: " + message + "\n");
        }
    }
}
